package com.xudow.app.directseeding.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bokecc.sdk.mobile.live.pojo.ChatMessage;
import com.bokecc.sdk.mobile.live.pojo.Viewer;

import com.xudow.app.R;
import com.xudow.app.directseeding.util.FaceStringUtil;

public class MyChatListViewAdapter extends BaseAdapter {

	private Context context;
	private List<ChatMessage> chatMsgs;
	private String viewerId;
	private String viewerName;

	public MyChatListViewAdapter(Context context, Viewer viewer, List<ChatMessage> chatMsgs) {
		this.context = context;
		this.chatMsgs = chatMsgs;
		this.viewerId = viewer.getId();
		this.viewerName = viewer.getName();
	}
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public int getCount() {
		return chatMsgs.size();
	}

	@Override
	public Object getItem(int position) {
		return chatMsgs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.lv_chat_view, parent, false);
			viewHolder.tv = (TextView) convertView.findViewById(R.id.tv1);
			viewHolder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ChatMessage chatMessage = chatMsgs.get(position);
		String userId = chatMessage.getUserId();

		if (chatMessage.isPublic()) {
			if (viewerId.equals(userId)) {

				viewHolder.tv.setText("我说：(公聊)");
			} else {

				viewHolder.tv.setText( chatMessage.getUserName());
			}

		} else {
			if (viewerId.equals(userId)) {

				viewHolder.tv.setText("我对主讲说：(私聊)");
			} else {

				viewHolder.tv.setText(chatMessage.getUserName() + "对我说：");
			}

		}

		viewHolder.tv2.setText(FaceStringUtil.parseFaceMsg(context, chatMessage.getMessage()));
		return convertView;
	}

	private class ViewHolder {
		public TextView tv;
		public TextView tv2;
	}
}
